// 用户相关类型
export interface User {
  id: number
  username: string
  email: string
  avatar_url: string
  role: UserRole
  student_id?: string
  real_name?: string
  school: string
  major?: string
  grade?: number
  email_verified: boolean
  last_login_at?: string
  created_at: string
}

export enum UserRole {
  STUDENT = 'student',
  TEACHER = 'teacher',
  ADMIN = 'admin'
}

export interface UserStats {
  solved_questions: number
  easy_solved: number
  medium_solved: number
  hard_solved: number
  total_submissions: number
  accepted_submissions: number
  accuracy: number
  contest_participated: number
  average_rank: number
  current_streak: number
  max_streak: number
  rating: number
  contribution: number
}

// 认证相关类型
export interface LoginForm {
  email: string
  password: string
}

export interface RegisterForm {
  username: string
  email: string
  password: string
  verification_code: string
  student_id?: string
  real_name?: string
  major?: string
  grade?: number
}

export interface AuthResponse {
  token: string
  user: User
}

// 题目相关类型
export interface Question {
  id: number
  title: string
  content: string
  description?: string
  input_format?: string
  output_format?: string
  constraints?: string
  sample_input?: string
  sample_output?: string
  difficulty: Difficulty
  time_limit: number
  memory_limit: number
  source?: string
  tags: Tag[]
  state?: string // 用户解题状态
  solved_count: number
  view_count: number
  like_count: number
  submission_count: number
  accepted_count: number
  acceptance_rate: number
  is_favorited?: boolean // 是否已收藏
  author?: {
    id: number
    username: string
  }
  user_status?: UserQuestionStatus
  solution?: Submission
  basecode?: Record<string, string>
  run_code?: Record<string, string>
  created_at: string
}

export enum Difficulty {
  EASY = 'Easy',
  MEDIUM = 'Medium',
  HARD = 'Hard'
}

export interface Tag {
  id: number
  name: string
  display_name: string
  color: string
  description?: string
}

export interface UserQuestionStatus {
  status: SolutionStatus
  submission_count: number
  first_solved_at?: string
  last_attempt_at?: string
}

export enum SolutionStatus {
  NOT_STARTED = 'not_started',
  ATTEMPTED = 'attempted',
  SOLVED = 'solved'
}

// 提交相关类型
export interface Submission {
  id: number
  user_id: number
  question_id: number
  question_title?: string
  language: ProgrammingLanguage
  code: string
  status: SubmissionStatus
  status_display: string
  score: number
  execution_time?: number
  memory_used?: number
  error_message?: string
  created_at: string
}

export interface SubmissionResult {
  submission_id: number
  status: SubmissionStatus
  execution_time: number
  memory_used: number
  error_message?: string
  beat_number: number
}

export enum ProgrammingLanguage {
  CPP = 'cpp',
  JAVA = 'java',
  PYTHON = 'python',
  PYTHON3 = 'python3',
  JAVASCRIPT = 'javascript',
  GO = 'go',
  RUST = 'rust',
  C = 'c'
}

export enum SubmissionStatus {
  PENDING = 'pending',
  JUDGING = 'judging',
  ACCEPTED = 'accepted',
  WRONG_ANSWER = 'wrong_answer',
  TIME_LIMIT_EXCEEDED = 'time_limit_exceeded',
  MEMORY_LIMIT_EXCEEDED = 'memory_limit_exceeded',
  RUNTIME_ERROR = 'runtime_error',
  COMPILE_ERROR = 'compile_error',
  SYSTEM_ERROR = 'system_error'
}

// 竞赛相关类型
export interface Contest {
  id: number
  title: string
  description?: string
  type: ContestType
  status: ContestStatus
  start_time: string
  end_time: string
  duration: number
  max_participants?: number
  participant_count: number
  is_public: boolean
  author?: {
    id: number
    username: string
  }
  questions?: Question[]
  created_at: string
}

export enum ContestType {
  INDIVIDUAL = 'individual',
  TEAM = 'team'
}

export enum ContestStatus {
  UPCOMING = 'upcoming',
  RUNNING = 'running',
  ENDED = 'ended'
}

export interface ContestParticipant {
  id: number
  contest_id: number
  user_id: number
  team_name?: string
  total_score: number
  solved_count: number
  penalty_time: number
  rank?: number
  last_submission_time?: string
  joined_at: string
  user?: User
}

// API响应类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data?: T
}

export interface PaginationResult<T = any> {
  items: any
  page: number
  page_size: number
  total: number
  total_pages: number
  has_next: boolean
  has_prev: boolean
  data: T[]
}

// 表单和查询类型
export interface QuestionFilters {
  page: number
  page_size: number
  tags?: string[]
  difficulty?: string[]
  status?: string[]
  search?: string
}

export interface RankingUser {
  rank: number
  id: number
  username: string
  avatar_url: string
  school: string
  solved_questions: number
  accuracy: number
  rating: number
  contest_participated: number
}

// UI相关类型
export interface Toast {
  id: string
  type: 'success' | 'error' | 'warning' | 'info'
  title: string
  message?: string
  duration?: number
}

export interface MenuItem {
  name: string
  path: string
  icon?: string
  children?: MenuItem[]
  meta?: {
    requireAuth?: boolean
    roles?: UserRole[]
    title?: string
  }
}

// 通用工具类型
export interface SelectOption {
  label: string
  value: string | number
  disabled?: boolean
}

export interface TableColumn {
  key: string
  label: string
  sortable?: boolean
  width?: string
  align?: 'left' | 'center' | 'right'
}

// 编辑器相关类型
export interface EditorTheme {
  name: string
  label: string
  isDark: boolean
}

export interface EditorSettings {
  theme: string
  fontSize: number
  tabSize: number
  wordWrap: boolean
  minimap: boolean
  lineNumbers: boolean
  autoSave: boolean
}

// 常量映射
export const DIFFICULTY_LABELS: Record<Difficulty, string> = {
  [Difficulty.EASY]: '简单',
  [Difficulty.MEDIUM]: '中等',
  [Difficulty.HARD]: '困难'
}

export const DIFFICULTY_COLORS: Record<Difficulty, string> = {
  [Difficulty.EASY]: 'text-green-600 bg-green-50',
  [Difficulty.MEDIUM]: 'text-yellow-600 bg-yellow-50',
  [Difficulty.HARD]: 'text-red-600 bg-red-50'
}

export const LANGUAGE_LABELS: Record<ProgrammingLanguage, string> = {
  [ProgrammingLanguage.CPP]: 'C++',
  [ProgrammingLanguage.JAVA]: 'Java',
  [ProgrammingLanguage.PYTHON]: 'Python 2',
  [ProgrammingLanguage.PYTHON3]: 'Python 3',
  [ProgrammingLanguage.JAVASCRIPT]: 'JavaScript',
  [ProgrammingLanguage.GO]: 'Go',
  [ProgrammingLanguage.RUST]: 'Rust',
  [ProgrammingLanguage.C]: 'C'
}

export const STATUS_COLORS: Record<SubmissionStatus, string> = {
  [SubmissionStatus.PENDING]: 'text-gray-600 bg-gray-50',
  [SubmissionStatus.JUDGING]: 'text-blue-600 bg-blue-50',
  [SubmissionStatus.ACCEPTED]: 'text-green-600 bg-green-50',
  [SubmissionStatus.WRONG_ANSWER]: 'text-red-600 bg-red-50',
  [SubmissionStatus.TIME_LIMIT_EXCEEDED]: 'text-yellow-600 bg-yellow-50',
  [SubmissionStatus.MEMORY_LIMIT_EXCEEDED]: 'text-orange-600 bg-orange-50',
  [SubmissionStatus.RUNTIME_ERROR]: 'text-purple-600 bg-purple-50',
  [SubmissionStatus.COMPILE_ERROR]: 'text-pink-600 bg-pink-50',
  [SubmissionStatus.SYSTEM_ERROR]: 'text-gray-600 bg-gray-50'
}

